#!/bin/bash
# 环境验证脚本
# 按照手册要求验证开发环境是否正确安装

echo "=== RISC-V 开发环境验证脚本 ==="
echo "按照手册要求验证以下工具："
echo

# 检查基础工具
echo "1. 检查基础工具："
echo -n "  git: "
if command -v git &> /dev/null; then
    git --version
else
    echo "❌ 未安装"
fi

echo -n "  make: "
if command -v make &> /dev/null; then
    make --version | head -1
else
    echo "❌ 未安装"
fi

echo -n "  python3: "
if command -v python3 &> /dev/null; then
    python3 --version
else
    echo "❌ 未安装"
fi

echo

# 检查RISC-V工具链
echo "2. 检查RISC-V工具链："
echo -n "  riscv64-unknown-elf-gcc: "
if command -v riscv64-unknown-elf-gcc &> /dev/null; then
    riscv64-unknown-elf-gcc --version | head -1
else
    echo "❌ 未安装"
fi

echo

# 检查QEMU
echo "3. 检查QEMU模拟器："
echo -n "  qemu-system-riscv64: "
if command -v qemu-system-riscv64 &> /dev/null; then
    qemu-system-riscv64 --version | head -1
else
    echo "❌ 未安装"
fi

echo

# 检查GDB
echo "4. 检查调试器："
echo -n "  gdb-multiarch: "
if command -v gdb-multiarch &> /dev/null; then
    gdb-multiarch --version | head -1
else
    echo "❌ 未安装"
fi

echo

# 测试交叉编译
echo "5. 测试交叉编译："
echo 'int main(){ return 0; }' > test.c
if riscv64-unknown-elf-gcc -c test.c -o test.o 2>/dev/null; then
    echo "  ✅ 交叉编译测试通过"
    file test.o
    rm -f test.c test.o
else
    echo "  ❌ 交叉编译测试失败"
    rm -f test.c test.o
fi

echo
echo "=== 验证完成 ==="
echo "如果所有工具都显示正确版本，说明环境搭建成功！"
echo "可以开始进行实验1：RISC-V引导与裸机启动"


